package com.mj.app.health;

import android.util.SparseArray;
import android.view.View;

/**
 * =====================
 * Created by David Hogan on 2018/2/12.
 * title: viewholder
 * description: 自定义缓存view 的holder
 * =====================
 */

public class MyViewHolder {

    /**
     * 自定义ViewHolder
     * @param convertView
     * @param id
     * @param <T>
     * @return 子View
     */
    public static <T extends View>T getView(View convertView, int id){
        SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag();

        View childView;
        if(viewHolder == null){
            viewHolder = new SparseArray<>();
            convertView.setTag(viewHolder);
            childView = convertView.findViewById(id);
            viewHolder.put(id, childView);
        }else{
            childView = viewHolder.get(id);
            if(childView == null){
                childView = convertView.findViewById(id);
                viewHolder.put(id, childView);
            }
        }
        return (T) childView;
    }
}
